#!/usr/bin/env ruby

def usage
    $stderr.puts "USAGE: #{File.basename($0)} <serial numbers file>"
    $stderr.puts "-"*40
    $stderr.puts " serial number file    File with card to serial number map"
    $stderr.puts 
    exit 42
end

snfile = ARGV.shift || usage
begin
    fdata = File.new(snfile, "rb")
rescue
    $stderr.puts "Unable to open #{snfile}. Abort"
    exit 1
end

cmd_fmt = "ruby utils/sn_scan.rb reader/data/%s %s > %s.%s"

fdata.each do |line|
    next if line =~ /^#/
    fname, sn1, sn2 = line.chomp.split(/\s+/)
    cmd = cmd_fmt % [fname, sn1, fname, "sn1"]
    `#{cmd}`
    cmd = cmd_fmt % [fname, sn2, fname, "sn2"]
    `#{cmd}`
end

fdata.close
